
/**
 * @author Thierry Bertin-Mahieux and Jin Yoshikawa
 * @data 02/05/2009
 * Screen where we can follow the robot
 */

import javax.swing.*;
import java.awt.geom.*;
import java.awt.Color;
import java.util.LinkedList;
import java.awt.*;


public class GPS extends JComponent {
	
	// keep a list of point, dimensions and the center
	LinkedList<Point2D> points = new LinkedList<Point2D>();
	int width = 250;
	int height = 250;
	double scaling = 4.;
	//JFrame embedder;
	
	Point2D center = new Point2D.Double(width * .3, height * .3);

	// default constructor
	public GPS() {}
	
	// constructor
	public GPS(String s){
		JFrame frame = new JFrame("Robot GPS");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //this.setPreferredSize( new Dimension(width,height) );
        this.setSize(new Dimension(width,height));
        frame.setSize(this.getWidth() + 20 , this.getHeight() + 20);
        //frame.setLayout(new FlowLayout());
        frame.getContentPane().add(this);
        frame.setLocation(400,20);
		frame.setVisible(true);
	}
	
	
	// paint all points
	protected void alllines(Graphics2D g2) {
		//System.out.println("alllines");
		int list_size = points.size();
		if (list_size <= 1)
			return;
		for (int k = 1; k < list_size; k++)
			oneline(points.get(k-1),points.get(k),g2,k-1);
	}
	
	// paint one line
	protected void oneline(Point2D p1, Point2D p2, Graphics2D g2,Integer num) {
		g2.setColor(Color.RED);
		double cx = center.getX();
		double cy = center.getY();
		g2.drawLine((int)(p1.getX()*scaling+cx), (int)(p1.getY()*scaling+cy), (int)(p2.getX()*scaling+cx), (int)(p2.getY()*scaling+cy));
		g2.drawString(num.toString(),(int)(p1.getX()*scaling+cx), (int)(p1.getY()*scaling+cy));
		//System.out.println("we draw line ("+(int)(p1.getX()*scaling+cx)+","+(int)(p1.getY()*scaling+cy)+","+(int)(p2.getX()*scaling+cx)+","+(int)(p2.getY()*scaling+cy));
	}
	
	// add point
	public void addPoint(float x, float y) {
		//System.out.println("we add point: ("+x+","+y+")");
		if (points.size() > 0)
		{
			if (x != points.get(points.size()-1).getX() || y != points.get(points.size()-1).getY())
				points.add( new Point2D.Float( x , y ) );
			else
				return;
		}
		else points.add( new Point2D.Float(x,y) );
		if (OutOfBounds())
			scaling = scaling * .85;
		if (points.size() > 1) {
			//System.out.println("we repaint from addPoint");
			this.repaint(10);
		}
		System.out.println("points size = " + points.size());
	}
	
	// are we too big?
	protected boolean OutOfBounds() {
		int h = this.getHeight();
		int w = this.getWidth();
		for (Point2D p : points)
		{
			if (p.getX() * scaling + center.getX() >= w ||
					p.getX() * scaling + center.getX() <= 0)
				return true;
			if (p.getY() * scaling + center.getY() >= h ||
					p.getY() * scaling + center.getY() <= 0)
				return true;
		}
		return false;
	}
	
	// repaint
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		//System.out.println("paintCmponent");
		Graphics2D g2 = (Graphics2D)g;
		//g2.setBackground(Color.BLACK);
		int w = this.getWidth();
        int h = this.getHeight();
        g2.setColor(Color.black);
        g2.fillRect(0, 0, w, h);
        alllines(g2);
	}
	
}


